package com.dong.yi18health;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dong.utils.Uri2String;
import com.squareup.picasso.Picasso;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
import fragment.News2;

public class DetailActivity extends Activity {

	public TextView title;
	public ImageView img;
	public WebView web;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail);
		initView();
		init();
	}

	private void initView() {
		title = (TextView) findViewById(R.id.detail_title);
		img = (ImageView) findViewById(R.id.detail_img);
		web = (WebView) findViewById(R.id.detail_web);
	}
	//网络数据的加载
	Handler hand=new Handler(){
		public void handleMessage(android.os.Message msg) {
			String result=	(String) msg.obj;
			JSONObject o = JSON.parseObject(result);
			String string = o.getString("yi18");
			JSONObject o2 = JSON.parseObject(string);
			String web2=o2.getString("message");
			String img2=o2.getString("img");
			String title2=o2.getString("title");
			title.setText(title2);
			Picasso.with(getBaseContext()).load("http://www.yi18.net/"+img2).into(img);
			//给webView添加数据
			web.loadData(web2, "text/html;charset=utf-8", null);
		};
	};
	private void init() {
		Intent intent = getIntent();
		final int id = intent.getIntExtra("id", 0);
		//得到意图中传递过来的id并开子线程执行下载任务
		if(id!=0){
			new Thread(){
				public void run() {
					String result = Uri2String.getResult("http://api.1ccf.com/news/show?id="+id);
					Message m=new Message();
					m.obj=result;
					hand.sendMessage(m);
				};
			}.start();
		}
	}
	
}
